Conditions | 25 |
Paths | 15 |
Total Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like stateGetter.js ➔ stateGetter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* |
||
13 | export function stateGetter(state, props, key, entry) { |
||
14 | |||
15 | if (props |
||
16 | && props.reducerKeys |
||
17 | && Object.keys(props.reducerKeys).length > 0 |
||
18 | && props.reducerKeys[key]) { |
||
19 | |||
20 | const dynamicKey = props.reducerKeys[key]; |
||
21 | |||
22 | const dynamicState = state |
||
23 | && state[dynamicKey] |
||
24 | && state[dynamicKey].get |
||
25 | && state[dynamicKey].get(entry) |
||
26 | ? state[dynamicKey].get(entry) |
||
27 | : null; |
||
28 | |||
29 | return dynamicState && |
||
30 | dynamicState.toJS |
||
31 | ? dynamicState.toJS() |
||
32 | : dynamicState; |
||
33 | } |
||
34 | |||
35 | const firstTry = state |
||
36 | && state[key] |
||
37 | && state[key].get |
||
38 | && state[key].get(entry) |
||
39 | ? state[key].get(entry) |
||
40 | : null; |
||
41 | |||
42 | if (firstTry) { |
||
43 | return firstTry.toJS ? firstTry.toJS() : firstTry; |
||
44 | } |
||
45 | |||
46 | const keys = Object.keys(state); |
||
47 | const normalizedKeys = keys |
||
48 | .map((s) => s.toLowerCase()); |
||
49 | |||
50 | const keyIndex = normalizedKeys.indexOf(key.toLowerCase()); |
||
51 | |||
52 | if (keyIndex !== -1) { |
||
53 | const secondTry = state |
||
54 | && state[keys[keyIndex]] |
||
55 | && state[keys[keyIndex]].get |
||
56 | && state[keys[keyIndex]].get(entry) |
||
57 | ? state[keys[keyIndex]].get(entry) |
||
58 | : null; |
||
59 | |||
60 | if (!state[keys[keyIndex]]) { |
||
61 | /* eslint-disable no-console */ |
||
62 | console.warn([ |
||
63 | 'Case insensitivity for reducer keys will no longer', |
||
64 | 'be supported in the next major release.', |
||
65 | 'Please update your reducer keys', |
||
66 | 'to match the main exports.' |
||
67 | ]); |
||
68 | } |
||
69 | |||
70 | return secondTry && secondTry.toJS |
||
71 | ? secondTry.toJS() |
||
72 | : secondTry; |
||
73 | } |
||
74 | |||
75 | return null; |
||
76 | } |
||
77 |